/*
 * Configuracao.java
 *
 * Created on 02/07/2007, 09:55:00
 *
 */
package br.com.grillo.jturbonote;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import java.util.logging.Level;
import static br.com.grillo.jturbonote.Main.logger;

/**
 *
 * @author grillo
 */
;

public class Configuracao {

	private static Properties conf;
	public static File arquivoConf;


	static {

		arquivoConf = new File(GerenciadorNotas.dirBase, "configJTurboNote.properties");
		InputStream fis;
		try {
			if (arquivoConf.exists()) {
				fis = new FileInputStream(arquivoConf);
			} else {
				fis = "".getClass().getResourceAsStream("/br/com/grillo/jturbonote/resource/configJTurboNote.properties");
			}
			conf = new Properties();
			conf.load(fis);
			fis.close();
		} catch (IOException ioEx) {
			logger.log(Level.SEVERE, "Erro na Configuracao", ioEx);
		}
	}

	private Configuracao() {
	}

	static String getValor(String chave) {
		return getValor(chave, null);
	}

	static int getValorInt(String chave) {
		return Integer.parseInt(getValor(chave, null));
	}

	static String getValor(String chave, String valorDefault) {
		return conf.getProperty(chave, valorDefault);
	}

	static void escreve(String chave, String valor) {
		if (valor == null) {
			valor = "";
		}
		conf.setProperty(chave, valor);
		dump();
	}

	private static void dump() {
		try {
			OutputStream out = new FileOutputStream(arquivoConf, false);
			conf.store(out, "Arquivo de configuracao do jTurboNote");
			out.close();
		} catch (IOException ioEx) {
			logger.log(Level.SEVERE, "Erro ao 'dumpar' configuracao", ioEx);
		}
	}
}
